<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
            font-size: 14px;
        }
        .button:hover {
            background-color: #0056b3;
        }
        .button.danger {
            background-color: #dc3545;
        }
        .button.danger:hover {
            background-color: #c82333;
        }
        .button.success {
            background-color: #28a745;
        }
        .button.success:hover {
            background-color: #218838;
        }
        .input-group {
            margin: 10px 0;
        }
        .input-group input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
        }
        .result {
            background-color: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 4px;
            padding: 15px;
            margin-top: 15px;
            white-space: pre-wrap;
            font-family: monospace;
            max-height: 400px;
            overflow-y: auto;
        }
        .success {
            border-color: #28a745;
            background-color: #d4edda;
            color: #155724;
        }
        .error {
            border-color: #dc3545;
            background-color: #f8d7da;
            color: #721c24;
        }
        h1, h2 {
            color: #333;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            border-radius: 4px;
            padding: 10px;
            margin: 10px 0;
            color: #0c5460;
        }
    </style>
</head>
<body>
    <h1>管理员测试页面</h1>
    
    <div class="info">
        <strong>使用说明：</strong><br>
        • 此页面仅用于开发和测试环境<br>
        • 默认管理员账号：admin，密码：admin123<br>
        • 所有接口返回结果会显示在下方的结果区域
    </div>

    <div class="container">
        <h2>基础操作</h2>
        <button class="button success" onclick="initAdmin()">创建测试管理员</button>
        <button class="button" onclick="listAdmins()">查看管理员列表</button>
        <button class="button" onclick="initMultipleAdmins()">批量创建管理员</button>
    </div>

    <div class="container">
        <h2>密码管理</h2>
        <div class="input-group">
            <input type="text" id="usernameInput" placeholder="输入用户名" value="admin">
            <button class="button" onclick="resetPassword()">重置密码</button>
        </div>
    </div>

    <div class="container">
        <h2>账号管理</h2>
        <div class="input-group">
            <input type="text" id="deleteUsernameInput" placeholder="输入要删除的用户名">
            <button class="button danger" onclick="deleteAdmin()">删除管理员</button>
        </div>
    </div>

    <div class="container">
        <h2>登录测试</h2>
        <div class="input-group">
            <input type="text" id="loginUsername" placeholder="用户名" value="admin">
            <input type="password" id="loginPassword" placeholder="密码" value="admin123">
            <button class="button success" onclick="testLogin()">测试登录</button>
        </div>
    </div>

    <div class="container">
        <h2>操作结果</h2>
        <div id="result" class="result">等待操作...</div>
        <button class="button" onclick="clearResult()">清空结果</button>
    </div>

    <script>
        const baseUrl = window.location.origin + '/api';
        
        function showResult(data, isError = false) {
            const resultDiv = document.getElementById('result');
            resultDiv.className = 'result ' + (isError ? 'error' : 'success');
            resultDiv.textContent = JSON.stringify(data, null, 2);
        }

        function clearResult() {
            const resultDiv = document.getElementById('result');
            resultDiv.className = 'result';
            resultDiv.textContent = '等待操作...';
        }

        async function apiCall(url, method = 'GET', body = null) {
            try {
                const config = {
                    method: method,
                    headers: {
                        'Content-Type': 'application/json',
                    }
                };
                
                if (body) {
                    config.body = JSON.stringify(body);
                }
                
                const response = await fetch(baseUrl + url, config);
                const data = await response.json();
                
                console.log('API Response:', data);
                showResult(data, !response.ok);
                
                return data;
            } catch (error) {
                console.error('API Error:', error);
                showResult({error: error.message}, true);
            }
        }

        async function initAdmin() {
            await apiCall('/admin/test/init-admin', 'POST');
        }

        async function listAdmins() {
            await apiCall('/admin/test/list-admins');
        }

        async function initMultipleAdmins() {
            await apiCall('/admin/test/init-multiple-admins', 'POST');
        }

        async function resetPassword() {
            const username = document.getElementById('usernameInput').value;
            if (!username) {
                showResult({error: '请输入用户名'}, true);
                return;
            }
            await apiCall(`/admin/test/reset-password?username=${username}`, 'POST');
        }

        async function deleteAdmin() {
            const username = document.getElementById('deleteUsernameInput').value;
            if (!username) {
                showResult({error: '请输入要删除的用户名'}, true);
                return;
            }
            if (!confirm(`确定要删除管理员 "${username}" 吗？`)) {
                return;
            }
            await apiCall(`/admin/test/delete-admin?username=${username}`, 'DELETE');
        }

        async function testLogin() {
            const username = document.getElementById('loginUsername').value;
            const password = document.getElementById('loginPassword').value;
            
            if (!username || !password) {
                showResult({error: '请输入用户名和密码'}, true);
                return;
            }
            
            await apiCall('/admin/login', 'POST', {
                username: username,
                password: password
            });
        }

        // 页面加载时显示当前服务器地址
        window.onload = function() {
            console.log('管理员测试页面已加载');
            console.log('API Base URL:', baseUrl);
        };
    </script>
</body>
</html> 